package cn.mickey.cloud.service.impl;


import cn.mickey.cloud.mapper.HouseMapper;
import cn.mickey.cloud.entity.House;
import cn.mickey.cloud.entity.Page;
import cn.mickey.cloud.service.HouseService;
import cn.mickey.cloud.utils.FileAction;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;


@Service
public class HouseServiceImpl implements HouseService {
    @Resource
    private HouseMapper houseMapper;
    @Resource
    private FileAction fileAction;
    @Override
    public List<House> findHouseAll() {
        return houseMapper.findHouseAll();
    }

    @Override
    public Page page(int page, int limit) {
        List<House> list = houseMapper.page(page, limit);
        int count = houseMapper.count();
        return new Page(count, list);
    }

    @Override
    public void delList(long id) {
        delFile(id);
        houseMapper.del(id);
    }

    @Override
    public void delList(Collection<Long> ids) {
        for (Long id : ids) {
            delList(id);
        }
        houseMapper.delList(ids);

    }

    @Override
    public void add(House house) {
        houseMapper.save(house);
    }

    @Override
    public void update(House house) {
        houseMapper.update(house);
        //更新时删除旧文件
        //delFile(house.getHid());
    }

    @Override
    public House getOne(long id) {
        return houseMapper.getOne(id);
    }
    @Override
    public List<House> getAll() {
        return houseMapper.getAll();
    }


    /**
     * 删除本地文件
     * @param id
     */
    public void delFile(long id){
        House house = houseMapper.getOne(id);
        fileAction.delFile(house.getImg());
        String[]  images = house.getImages().split(",");
        for (String image: images) {
            fileAction.delFile(image);
        }
    }

}
